package com.shop.cloud.controller.activity;

import io.swagger.annotations.ApiOperation;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.Activity;
import com.shop.cloud.domain.hongfu.ActivityCategory;
import com.shop.cloud.domain.hongfu.ActivityEnroll;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.service.hongfu.ActivityCategoryService;
import com.shop.cloud.service.hongfu.IActivityService;
import com.shop.cloud.util.Page;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

/**
 * 活动管理
 * @author Yan Weiwei
 * 
 */
@RestController
@RequestMapping(value = "activity")
public class ActivityController extends BaseController {
	@Autowired
	private IActivityService activityService;
	
	@Autowired
	private ActivityCategoryService activityCategoryService;
	
	@ApiOperation(value = "获取活动列表", notes = "获取活动列表")
	@RequestMapping(value = "/activityList", method = RequestMethod.POST)
	@ResponseBody
	public Response activityList(@CurrentUserId Long userId, @RequestBody Activity activity){
		
		activityService.handleActivityStart();
		activityService.handleActivityEnd();
		
		activity.setExistFlag(0);
		activity.setOrderBy("ACTIVITY.create_time DESC");
		Page<Activity> data = activityService.page(activity);
		
		return ResponseUtil.ok(data);
	}
	
	@ApiOperation(value = "新增活动", notes = "新增活动")
	@RequestMapping(value = "/addActivity", method = RequestMethod.POST)
	@ResponseBody
	public Response addActivity(@CurrentUserId Long userId, @RequestBody Activity activity){
		
		boolean success = activityService.handleInsertActivity(activity);
		
		return ResponseUtil.ok(success);
	}
	
	@ApiOperation(value = "修改活动", notes = "修改活动")
	@RequestMapping(value = "/updateActivity", method = RequestMethod.POST)
	@ResponseBody
	public Response updateActivity(@CurrentUserId Long userId, @RequestBody Activity activity){
		
		int success = activityService.handleUpdateActivity(activity);
		
		return ResponseUtil.ok(success);
	}
	
	@ApiOperation(value = "删除活动", notes = "删除活动")
	@RequestMapping(value = "/deleteActivity", method = RequestMethod.GET)
	@ResponseBody
	public Response deleteActivity(@CurrentUserId Long userId, String activityID){
		
		int success = activityService.handleDeleteActivity(activityID);
		return ResponseUtil.ok(success);
	}
	
	@ApiOperation(value = "获取某个活动", notes = "获取某个活动")
	@RequestMapping(value = "/getActivity", method = RequestMethod.GET)
	@ResponseBody
	public Response getActivity(@CurrentUserId Long userId, String activityID){
		
		Activity insActivity = activityService.handleActivity(userId, activityID);
		
		return ResponseUtil.ok(insActivity);
	}
	
	@ApiOperation(value = "获取某个活动(包括报名列表)", notes = "获取某个活动(包括报名列表)")
	@RequestMapping(value = "/getActivityAndEnroll", method = RequestMethod.GET)
	@ResponseBody
	public Response getActivityAndEnroll(@CurrentUserId Long userId, String activityID){
		
		Activity insActivity = activityService.handleActivityAndEnroll(activityID);
		
		return ResponseUtil.ok(insActivity);
	}
	
	@ApiOperation(value = "获取活动下用户列表详细信息", notes = "获取活动下用户列表详细信息")
	@RequestMapping(value = "/getActivityAndEnrollMore", method = RequestMethod.GET)
	@ResponseBody
	public Response getActivityAndEnrollMore(@CurrentUserId Long userId, String activityID){
		
		List<ActivityEnroll> datas = activityService.handleActivityUserList(activityID);
		
		return ResponseUtil.ok(datas);
	}
	
	@ApiOperation(value = "活动退款", notes = "活动退款")
	@RequestMapping(value = "/activityRefund", method = RequestMethod.GET)
	@ResponseBody
	public Response activityRefund(String activityID, Long userId){
		
		activityService.handleActivityRefund(activityID, userId);
		
		return ResponseUtil.ok(0);
	}
	
	@ApiOperation(value = "活动分类列表", notes = "活动分类列表")
	@RequestMapping(value = "/activityCategory", method = RequestMethod.POST)
	@ResponseBody
	public Response activityCategory(@RequestBody ActivityCategory activityCategory){
		

		activityCategory.setOrderBy("ACTIVITY_CATEGORY.weigh_num");
		Page<ActivityCategory> data = activityCategoryService.page(activityCategory);
		
		return ResponseUtil.ok(data);
	}
	
	@ApiOperation(value = "新增分类", notes = "新增分类")
	@RequestMapping(value = "/addCategory", method = RequestMethod.POST)
	@ResponseBody
	public Response addCategory(@RequestBody ActivityCategory activityCategory){
		ActivityCategory activityCategory2=new ActivityCategory();
		activityCategory2.setWeighNum(activityCategory.getWeighNum());
		List<ActivityCategory> acList=activityCategoryService.find(activityCategory2);
		if(acList.size()>0){
			return ResponseUtil.ok(3);
		}
		
		activityCategory.setCreateTime(new Timestamp((new Date()).getTime()));
		
		boolean success = activityCategoryService.insert(activityCategory);
		if (success==true) {
			return ResponseUtil.ok(1);
		}
		
		if(success==false){
			return ResponseUtil.ok(0);
		}
		
		
		return ResponseUtil.ok(0);
	}
	
	@ApiOperation(value = "修改分类", notes = "修改分类")
	@RequestMapping(value = "/updateCategory", method = RequestMethod.POST)
	@ResponseBody
	public Response updateCategory(@RequestBody ActivityCategory activityCategory){
		ActivityCategory activityCategory2=new ActivityCategory();
		activityCategory2.setWeighNum(activityCategory.getWeighNum());
		List<ActivityCategory> acList=activityCategoryService.find(activityCategory2);
		if(acList.size()>0){
			if (!acList.get(0).getName().equals(activityCategory.getName())) {
				activityCategory.setUpdateTime(new Timestamp((new Date()).getTime()));
				
				int success = activityCategoryService.update(activityCategory);
				return ResponseUtil.ok(1);
			}
			
			return ResponseUtil.ok(3);
		}
		
		
		activityCategory.setUpdateTime(new Timestamp((new Date()).getTime()));
		
		int success = activityCategoryService.update(activityCategory);
		
		return ResponseUtil.ok(success);
	}
	
	
	@ApiOperation(value = "删除分类", notes = "删除分类")
	@RequestMapping(value = "/deleteCategory", method = RequestMethod.POST)
	@ResponseBody
	public Response deleteCategory(@RequestBody ActivityCategory activityCategory){
		
		int success = activityCategoryService.remove(activityCategory);
		
		return ResponseUtil.ok(success);
	}
}
